<?php
/**
 * (logic)（临时）MonthlyStorage月储存费
 */
namespace App\Logic\File\Profit;

use App\Models\Basics\SysCountry;
use App\Models\MonthlyReport\Payment\MonthlyStorage;
class ParseMonthlyStorageFile
{
    /**
     * 解析MonthlyStorage
     * @param $lines
     * @param $date
     * @return int
     */
    public static function exeParse($lines,$date){
        $count = count($lines);
        $i = 0;
        $num = 100;
        $re_count = 0;
        while(true){
            if(empty($lines))break;
            $_lines = array_splice($lines,$i,$num);
            foreach ($_lines as $line){
                $flag = self::_addMonthlyStorage($line,$date);
                $flag && $re_count++;
            }
        }
        return $re_count == $count ? 1 : 0;
    }

    private static function _addMonthlyStorage($temp,$date){
        if(count($temp) < 5)return true;
        if(!$temp[0])return true;
        $countryId = SysCountry::getIdByCountry($temp[1]);

        //MonthlyStorage月储存费
        $re = MonthlyStorage::create([
            'report_date' => $date,
            'account' => str_replace('ZH', '账号', strtoupper($temp[0])),
            'country' => strtoupper($temp[1]),
            'country_id' => $countryId ?: 0,
            'asin' => $temp[2],
            'fnsku' => $temp[3],
            'estimated_monthly_storage_fee' => $temp[4],
        ]);
        return !$re ? false : true;
    }
}
